package com.codejiwei.core.graphx

import org.apache.spark.SparkContext
import org.apache.spark.graphx.{Edge, Graph}

object GraphX_PageRank {
  def main(args: Array[String]): Unit = {

    // 创建 SparkContext
    val sc = new SparkContext("local", "GraphPageRankExample")

    // 创建一个简单的图
    val vertices = sc.parallelize(Array((1L, "Alice"), (2L, "Bob"), (3L, "Charlie"), (4L, "David")))
    val edges = sc.parallelize(Array(
      Edge(1L, 2L, 1), // 边1: Alice -> Bob，权重为1
      Edge(1L, 3L, 1), // 边2: Alice -> Charlie，权重为1
      Edge(2L, 3L, 1), // 边3: Bob -> Charlie，权重为1
      Edge(3L, 4L, 1), // 边4: Charlie -> David，权重为1
      Edge(4L, 1L, 1) // 边5: David -> Alice，权重为1
    ))

    val graph: Graph[String, Int] = Graph(vertices, edges, "default")

    //TODO 衡量网络中节点的重要性
    // 执行 PageRank 算法
    val pageRankGraph = graph.pageRank(tol = 0.0001)

    // 输出每个顶点的 PageRank 值
    pageRankGraph.vertices.collect().foreach { case (vertexId, pageRank) =>
      println(s"Vertex $vertexId's PageRank: $pageRank")
    }

  }
}
